package ysq.xyg.host.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.host.dao.HostShopDao;
import ysq.xyg.host.model.po.HostShop;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.host.model.vo.HostShopVo;
import ysq.xyg.host.service.HostShopService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 房产模块-门店管理service实现
 *
 * @author congcong tao
 * @date 2019-8-30 10:35:07
 */
@Service("hostShopService")
public class HostShopServiceImpl extends BaseServiceImpl<HostShop> implements HostShopService {

    @Resource
    private HostShopDao hostShopDao;

    private HostShop hostShop;

    @Autowired
    public void setBaseDao(HostShopDao hostShopDao) {
        this.baseDao = hostShopDao;
    }


    /**
     * 分页查询
     *
     * @param hostShopVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-8-30 10:35:07
     */
    public ReturnEntity page(HostShopVo hostShopVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<HostShop> list = acceptList(hostShopVo);
        PageInfo<HostShop> page=new PageInfo<HostShop>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostShopVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-8-30 10:35:07
     */
    public ReturnEntity addOrUpdate(HostShopVo hostShopVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(hostShopVo.getId())){
            hostShop=new HostShop(currentUser);
            isUpate=false;
        }else{
            hostShop=selectByPrimaryKey(hostShopVo.getId());
            if(ObjectUtil.isNull(hostShop)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            hostShop.setUpdateTime(new Date());
            hostShop.setUpdateUser(currentUser.getId());
        }
        hostShop.setNumber(hostShopVo.getNumber());
        hostShop.setShopName(hostShopVo.getShopName());
        hostShop.setName(hostShopVo.getName());
        hostShop.setPhone(hostShopVo.getPhone());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(hostShop);
        }else{
            num=insert(hostShop);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-8-30 10:35:07
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostShop hostShop=selectByPrimaryKey(id);
        if(hostShop!=null){
            hostShop.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostShop.setUpdateTime(new Date());
            hostShop.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(hostShop);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostShopVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-8-30 10:35:07
     */
    public List<HostShop> acceptList(HostShopVo hostShopVo){
        Example example = new Example(HostShop.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(hostShopVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",hostShopVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return hostShopDao.selectByExample(example);
    }
}
